home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWPictur.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  10.7 KB  |  401 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPictur.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWPICTUR_H
  13. #include "FWPictur.h"
  14. #endif
  15.  
  16. #ifndef SLGRGLOB_H
  17. #include "SLGrGlob.h"
  18. #endif
  19.  
  20. #ifndef FWGRUTIL_H
  21. #include "FWGrUtil.h"
  22. #endif
  23.  
  24. #ifndef FWGC_H
  25. #include "FWGC.h"
  26. #endif
  27.  
  28. #ifndef FWODGEOM_H
  29. #include "FWODGeom.h"
  30. #endif
  31.  
  32. // ----- OS Includes -----
  33.  
  34. #ifndef FWRESACC_H
  35. #include "FWResAcc.h"
  36. #endif
  37.  
  38. #ifndef FWRESSIN_H
  39. #include "FWResSin.h"
  40. #endif
  41.  
  42. #ifndef FWACQUIR_H
  43. #include "FWAcquir.h"
  44. #endif
  45.  
  46. // ----- Foundation Includes -----
  47.  
  48. #ifndef FWSOMENV_H
  49. #include "FWSOMEnv.h"
  50. #endif
  51.  
  52. #ifndef FWSTREAM_H
  53. #include "FWStream.h"
  54. #endif
  55.  
  56. #ifndef FWDEBUG_H
  57. #include "FWDebug.h"
  58. #endif
  59.  
  60. #ifndef FWSTRMRW_H
  61. #include "FWStrmRW.h"
  62. #endif
  63.  
  64. #ifndef FWMEMMGR_H
  65. #include "FWMemMgr.h"
  66. #endif
  67.  
  68. // ----- OpenDoc Includes -----
  69.  
  70. #ifndef SOM_ODCanvas_xh
  71. #include <Canvas.xh>
  72. #endif
  73.  
  74. #ifndef SOM_ODShape_xh
  75. #include <Shape.xh>
  76. #endif
  77.  
  78. #ifndef SOM_ODTransform_xh
  79. #include <Trnsform.xh>
  80. #endif
  81.  
  82. // ----- Macintosh Includes -----
  83.  
  84. #if defined(FW_BUILD_MAC) && !defined(__RESOURCES__)
  85. #include <Resources.h>
  86. #endif
  87.  
  88. //========================================================================================
  89. //    RunTime Info
  90. //========================================================================================
  91.  
  92. #ifdef FW_BUILD_MAC
  93. #pragma segment FWGraphics_Picture
  94. #endif
  95.  
  96. //========================================================================================
  97. //    Template instantiation
  98. //========================================================================================
  99.  
  100. #include "FWGrRef.tpp"
  101.  
  102. FW_DEFINE_AUTO_TEMPLATE(FW_TGrRefPtr, FW_HPicture)
  103.  
  104. #ifdef FW_USE_TEMPLATE_PRAGMAS
  105.  
  106. #pragma template_access public
  107. #pragma template FW_TGrRefPtr<FW_HPicture>
  108.  
  109. #else
  110.  
  111. template class FW_TGrRefPtr<FW_HPicture>;
  112.  
  113. #endif
  114.  
  115. //========================================================================================
  116. //    class FW_CPicture
  117. //========================================================================================
  118.  
  119. FW_DEFINE_AUTO(FW_CPicture)
  120.  
  121. //----------------------------------------------------------------------------------------
  122. //    FW_PrivAcquireGrRep
  123. //----------------------------------------------------------------------------------------
  124.  
  125. void FW_PrivAcquireGrRep(FW_HPicture rep)
  126. {
  127.     if (rep != 0)
  128.         FW_PrivPicture_Acquire(rep);
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. //    FW_PrivReleaseGrRep
  133. //----------------------------------------------------------------------------------------
  134.  
  135. void FW_PrivReleaseGrRep(FW_HPicture rep)
  136. {
  137.     if (rep != 0)
  138.         FW_PrivPicture_Release(rep);
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. //    FW_CPicture::FW_CPicture
  143. //----------------------------------------------------------------------------------------
  144.  
  145. FW_CPicture::FW_CPicture()
  146. {
  147.     FW_END_CONSTRUCTOR
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. //    FW_CPicture::FW_CPicture
  152. //----------------------------------------------------------------------------------------
  153.  
  154. FW_CPicture::FW_CPicture(FW_PlatformPict platformPict)
  155. {
  156.     FW_PlatformError error;
  157.     FW_HPicture rep = FW_PrivPicture_CreateFromPlatformPict(platformPict, &error);
  158.     FW_FailOnError(error);
  159.     SetRep(rep);
  160.     FW_END_CONSTRUCTOR
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. //    FW_CPicture::FW_CPicture
  165. //----------------------------------------------------------------------------------------
  166.  
  167. FW_CPicture::FW_CPicture(FW_PResourceFile& resourceFile, FW_ResourceId resourceId)
  168. {
  169.     FW_PlatformError error;
  170.     FW_HPicture rep = FW_PrivPicture_CreateFromResource(resourceFile, resourceId, &error);
  171.     FW_FailOnError(error);
  172.     SetRep(rep);
  173.     FW_END_CONSTRUCTOR
  174. }
  175.  
  176. //----------------------------------------------------------------------------------------
  177. //    FW_CPicture::FW_CPicture
  178. //----------------------------------------------------------------------------------------
  179.  
  180. FW_CPicture::FW_CPicture(const FW_CPicture& other) :
  181.     FW_TGrRefPtr<FW_HPicture>(other)
  182. {
  183.     FW_END_CONSTRUCTOR
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. //    FW_CPicture::FW_CPicture
  188. //----------------------------------------------------------------------------------------
  189.  
  190. FW_CPicture::FW_CPicture(FW_CReadableStream& stream)
  191. {
  192.     FW_PlatformError error;
  193.     FW_HPicture rep = FW_PrivPicture_Read(stream, &error);
  194.     FW_FailOnError(error);
  195.     SetRep(rep);
  196.     FW_END_CONSTRUCTOR
  197. }
  198.  
  199. //----------------------------------------------------------------------------------------
  200. //    FW_CPicture::~FW_CPicture
  201. //----------------------------------------------------------------------------------------
  202.  
  203. FW_CPicture::~FW_CPicture()
  204. {
  205.     FW_START_DESTRUCTOR
  206. }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. //    FW_CPicture::operator=
  210. //----------------------------------------------------------------------------------------
  211.  
  212. FW_CPicture& FW_CPicture::operator=(const FW_CPicture& other)
  213. {
  214.     FW_TGrRefPtr<FW_HPicture>::operator=(other);
  215.     return *this;
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. //    FW_CPicture::Copy
  220. //----------------------------------------------------------------------------------------
  221.  
  222. FW_CPicture FW_CPicture::Copy() const
  223. {
  224.     FW_PlatformError error;
  225.     FW_HPicture rep = FW_PrivPicture_Copy(fRep, &error);
  226.     FW_FailOnError(error);
  227.     
  228.     FW_CPicture picture;
  229.     picture.SetRep(rep);
  230.     return picture;
  231. }
  232.  
  233. //----------------------------------------------------------------------------------------
  234. //    FW_CPicture::Copy
  235. //----------------------------------------------------------------------------------------
  236.  
  237. void FW_CPicture::GetPictBounds(FW_SGraphicContext& gc, FW_SRect& bounds) const
  238.     FW_PrivPicture_GetPictBoundsGC(gc.fEnvironment, fRep, gc, bounds); 
  239.     FW_FailOnEvError(gc.fEnvironment);
  240. }
  241.  
  242.  
  243. //========================================================================================
  244. //    Stream I/O
  245. //========================================================================================
  246.  
  247. //----------------------------------------------------------------------------------------
  248. //    operator>>
  249. //----------------------------------------------------------------------------------------
  250.  
  251. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_CPicture& picture)
  252. {
  253.     FW_PlatformError error;
  254.     FW_HPicture rep = FW_PrivPicture_Read(stream, &error);
  255.     FW_FailOnError(error);
  256.     picture.SetRep(rep);
  257.     return stream;
  258. }
  259.  
  260. //----------------------------------------------------------------------------------------
  261. //    operator<<
  262. //----------------------------------------------------------------------------------------
  263.  
  264. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_CPicture& picture)
  265. {
  266.     FW_PlatformError error;
  267.     FW_PrivPicture_Write(picture.fRep, stream, &error);
  268.     FW_FailOnError(error);
  269.     return stream;
  270. }
  271.  
  272. //========================================================================================
  273. //    class FW_CPictureContext
  274. //========================================================================================
  275.  
  276. FW_DEFINE_AUTO(FW_CPictureContext)
  277.  
  278. //----------------------------------------------------------------------------------------
  279. //    FW_CPictureContext::FW_CPictureContext
  280. //----------------------------------------------------------------------------------------
  281.  
  282. FW_CPictureContext::FW_CPictureContext(Environment* ev,
  283.                                        FW_CPicture& picture,
  284.                                        FW_Fixed xSize,
  285.                                        FW_Fixed ySize) :
  286.     FW_CGraphicContext(ev),
  287. #ifdef FW_BUILD_MAC
  288.     fMacPictHandle(NULL),
  289. #endif
  290.     fPicture(picture),
  291.     fGraphicDevice(NULL)
  292. {
  293.     ODPlatformCanvas platformCanvas;
  294.  
  295.     FW_CRect clipRect;
  296.     
  297. #ifdef FW_BUILD_MAC
  298.     platformCanvas = FW_gScratchPort;
  299.     clipRect.Set(FW_IntToFixed(0), FW_IntToFixed(0), xSize, ySize);
  300. #endif
  301.  
  302. #ifdef FW_BUILD_WIN
  303.     HDC hDCRef = ::GetDC(NULL);
  304.     
  305.     int xRes = ::GetDeviceCaps(hDCRef, LOGPIXELSX);
  306.     int yRes = ::GetDeviceCaps(hDCRef, LOGPIXELSY);
  307.     
  308.     int xDevMM = ::GetDeviceCaps(hDCRef, HORZSIZE);
  309.     int yDevMM = ::GetDeviceCaps(hDCRef, VERTSIZE);
  310.     int xDevPix = ::GetDeviceCaps(hDCRef, HORZRES);
  311.     int yDevPix = ::GetDeviceCaps(hDCRef, VERTRES);
  312.  
  313.     // The size comes in at 72dpi: convert to HiMetric units (0.01 mm)
  314.     FW_CPlatformRect boundsMM(0, 0, FW_FixedToInt(xSize) * 2540 / 72, FW_FixedToInt(ySize) * 2540 / 72);
  315.  
  316.     platformCanvas = ::CreateEnhMetaFile(hDCRef, NULL, &boundsMM, NULL);
  317.  
  318.     // Convert size to pixels, for use in the coordinate system
  319.     int xSizePix = 
  320.         FW_FixedToInt(FW_WideMultiply(xSize, FW_IntToFixed(xRes)) / FW_kFixed72);
  321.  
  322.     int ySizePix =
  323.         FW_FixedToInt(FW_WideMultiply(ySize, FW_IntToFixed(yRes)) / FW_kFixed72);
  324.  
  325.     int xDraw = (254 * xSizePix * xDevPix) / (100 * xDevMM);
  326.     int yDraw = (254 * ySizePix * yDevPix) / (100 * yDevMM);
  327.  
  328.     clipRect.SetInt(0, 0, xDraw, yDraw);
  329.  
  330.     ::ReleaseDC(NULL, hDCRef);
  331. #endif
  332.  
  333.     fGraphicDevice = FW_PrivGDev_CreateFromPlatformCanvas(ev, platformCanvas);
  334.     FW_FailOnEvError(ev);
  335.  
  336. #ifdef FW_BUILD_WIN
  337.     // Set the resolution: (xSize, ySize) should map to (xDraw, yDraw)
  338.     FW_PrivGDev_SetResolution(fGraphicDevice,
  339.         FW_IntToFixed(boundsMM.right) / FW_IntToFixed(xDraw),
  340.         FW_IntToFixed(boundsMM.bottom) / FW_IntToFixed(yDraw)
  341.         );
  342. #endif
  343.  
  344.     FW_CAcquiredODShape aqClipShape = ::FW_NewODShape(ev, clipRect);
  345.  
  346.     InitGraphicContext(fGraphicDevice,
  347.                        NULL,
  348.                        aqClipShape);
  349.  
  350. #ifdef FW_BUILD_MAC
  351.     FW_CPlatformRect plfmBounds(0, 0, FW_FixedToInt(xSize), FW_FixedToInt(ySize));
  352.  
  353.     ::PortSize(plfmBounds.right, plfmBounds.bottom);
  354.     fMacPictHandle = ::OpenPicture(&plfmBounds);
  355. #endif
  356.  
  357.     FW_END_CONSTRUCTOR
  358. }
  359.  
  360. //----------------------------------------------------------------------------------------
  361. //    FW_CPictureContext::~FW_CPictureContext
  362. //----------------------------------------------------------------------------------------
  363.  
  364. FW_CPictureContext::~FW_CPictureContext()
  365. {
  366.     FW_START_DESTRUCTOR
  367.  
  368.     FW_PlatformPict newPict;
  369.  
  370. #ifdef FW_BUILD_MAC
  371.     ::ClosePicture();
  372.     newPict = fMacPictHandle;
  373. #endif
  374.  
  375. #ifdef FW_BUILD_WIN
  376.     HDC hDC = FW_PrivGDev_GetPlatformCanvas(fGraphicDevice);
  377. #endif
  378.  
  379.     // ----- Terminate the superclass
  380.     TerminateGraphicContext();
  381.  
  382. #ifdef FW_BUILD_WIN
  383.     newPict = ::CloseEnhMetaFile(hDC);
  384. #endif
  385.  
  386.     // ----- Delete the device
  387.     FW_PrivGDev_Release(fGraphicDevice);
  388.  
  389.     // ----- Store the result in the picture
  390.     fPicture = FW_CPicture(newPict);;
  391. }
  392.  
  393. //----------------------------------------------------------------------------------------
  394. //    FW_CPictureContext::Reset
  395. //----------------------------------------------------------------------------------------
  396.  
  397. void FW_CPictureContext::Reset()
  398. {
  399.     // Nothing to do [HLX] Am i sure ?
  400. }